package userInterface;
import dataProces.DatabaseOperations;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Investment extends JDialog implements ActionListener {
    JTextField[] jFields = new JTextField[10];
    String[] prompt = { "金", "银", "铜", "铁", "余额查询", "金额计算", "购买", "出售", "返回", "购物界面" };
    double[] price = { 500, 50, 5, 0.5 };
    String[] priceTxt = new String[4];
    double[] weight ={0,0,0,0};
    JPanel[] jPanels = new JPanel[6];
    JButton[] jButtons = new JButton[5];
    String[] goods={"gold","silver","copper","iron"};
    DatabaseOperations dOperations = new DatabaseOperations();
    public Investment() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(6, 1));
        for (int i = 0; i < jPanels.length; i++) {
            jPanels[i] = new JPanel();
            if (i < 4)
                for (int j = i * 2; j < (i + 1) * 2; j++) {
                    jFields[j] = new JTextField(15);
                    if (j % 2 == 0) {
                        jPanels[i].add(new JLabel("当前持有"));
                        jFields[j].setEditable(false);
                        jFields[j].setText(dOperations.dataQueries("investment", Login.user,goods[i]));
                    } else {
                        priceTxt[i] = String.format("%6.2f", price[i] + price[i] * (Math.random() - 0.5) / 10);
                        price[i] = Double.parseDouble(priceTxt[i]);
                        jPanels[i].add(new JLabel(prompt[i] +"价(" + priceTxt[i] + "元/克)"));
                        jFields[j].setText("0");
                    }
                    jPanels[i].add(jFields[j]);
                }
            else if (i == 4)
                for (int j = 0; j < 2; j++) {
                    jButtons[j] = new JButton(prompt[i + j]);
                    jButtons[j].addActionListener(this);
                    jPanels[i].add(jButtons[j]);
                    jFields[i * 2 + j] = new JTextField(15);
                    jFields[i * 2 + j].setEditable(false);
                    jPanels[i].add(jFields[i * 2 + j]);
                }
            else
                for (int j = 2; j < jButtons.length; j++) {
                    jButtons[j] = new JButton(prompt[i + j - 1]);
                    jButtons[j].addActionListener(this);
                    jPanels[i].add(jButtons[j]);
                }
            container.add(jPanels[i]);
        }

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(500, 300);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - 500) / 2, (screen.height - 300) / 2);
        setTitle(prompt[8]);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButtons[0]) {
            String money=dOperations.dataQueries("investment", Login.user, "money");
            jFields[8].setText(money);
        } else if (e.getSource() == jButtons[1]) {
            double money = 0;
            for (int i = 1,j=0; i < 8; i += 2,j++)
                money += Double.parseDouble(jFields[i].getText().trim())*price[j];
            jFields[9].setText(String.format("%.2f",money));
        } else if (e.getSource() == jButtons[2]) {
            double font = 0;
            for (int i = 1,j=0; i < 8; i += 2,j++)
                font += Double.parseDouble(jFields[i].getText().trim())*price[j];
            double money = Double.parseDouble(dOperations.dataQueries("investment", Login.user, "money"));
            if(money<font){
                JOptionPane.showMessageDialog(null, "余额不足！");
            }
            else{
                money-=font;
                for(int i=0;i<goods.length;i++){
                    weight[i]=Double.parseDouble(dOperations.dataQueries("investment",Login.user,goods[i]));
                    weight[i]+=Double.parseDouble(jFields[2*i+1].getText().trim());
                    dOperations.dataUpdates("investment", Login.user, goods[i],String.format("%.0f",weight[i]));
                }
                dOperations.dataUpdates("investment", Login.user, "money", String.format("%.2f",money));
                JOptionPane.showMessageDialog(null,"购买花费"+String.format("%.2f",font)+"元！");
                dispose();
                new Investment();
            }
        } else if (e.getSource() == jButtons[3]) {
            double money=Double.parseDouble(dOperations.dataQueries("investment",Login.user,"money"));
            for(int i=0;i<goods.length;i++){
                weight[i]=Double.parseDouble(dOperations.dataQueries("investment",Login.user,goods[i]));
                double t=Double.parseDouble(jFields[2*i+1].getText().trim());
                if(weight[i]<t){
                    JOptionPane.showMessageDialog(null, prompt[i]+"持有量不足！");
                }
                else if(t>0){
                    money+=t*price[i];
                    weight[i]-=t;
                    dOperations.dataUpdates("investment", Login.user, goods[i],String.format("%.0f",weight[i]));
                    dOperations.dataUpdates("investment", Login.user, "money", String.format("%.2f",money));
                    JOptionPane.showMessageDialog(null,"卖出"+prompt[i]+"获得"+String.format("%.2f",t*price[i])+"元！");
                }
            }
            dispose();
            new Investment();
        } else if (e.getSource() == jButtons[4]) {
            dispose();
            new UserMenu();
        }
    }

    public static void main(String[] args) {
        new Investment();
    }
}
